package cn.nawang.ebeim.uc;
import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.embedded.browser.ChromeSettings;
import org.embedded.browser.Chromium;

import cn.nawang.ebeim.client.util.PathUtil;
import cn.nawang.ebeim.uc.util.ConfigUtil;  
  
  
public class CustomDialog extends Dialog {  
    protected Object result;  
    protected Shell shell ; 
    private Chromium chromium;
    private Composite titleComposite;
    private Composite mainComposite;
    private Display display;
    private Label closeLl;
    private static final int WIDTH = 920;
    private static final int HEIGHT = 680;
    private String title;
    private String url;
    private Rectangle displayRect;
    private Label titleLl;
    private Label ybaLl;
  
    public String getTitle() {
      return title;
    }
    public void setTitle(String title) {
      this.title = title;
    }
    
    public String getUrl() {
      return url;
    }
    public void setUrl(String url) {
      this.url = url;
    }
    
    private String path(){
      File rootDir = new File(PathUtil.getClassPath());
      String imgPath = rootDir + "/others/";
      return imgPath;
    }
    public CustomDialog(Shell parent,String title) {  
        super(parent, SWT.NO_TRIM);  
        this.title = title;
        CustomDialogInit();//初始化弹出窗口
    }  
  
    public Object open() {  
        showPage();  
        shell.open();  
        shell.layout();  
        while (!shell.isDisposed()) {  
            if (!display.readAndDispatch())  
                display.sleep();  
        }  
        return result;  
    }  
  
    protected void CustomDialogInit(){
      display = getParent().getDisplay();
      displayRect = display.getBounds();
      shell = new Shell(getParent(), SWT.NO_TRIM|SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
      shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
      shell.setSize(WIDTH, HEIGHT);  
      shell.setBounds((displayRect.width - WIDTH) >> 1,(displayRect.height - HEIGHT) >> 1, WIDTH, HEIGHT);
      RGB lineRgb = new RGB(20, 51, 83);
      Color lineColor = new Color(Display.getDefault(), lineRgb);
      shell.setBackground(lineColor);
      titleComposite = new Composite(shell, SWT.INHERIT_DEFAULT);
      titleComposite.setBounds(1, 1, WIDTH-2, 29);
      RGB rgb = new RGB(17, 110, 185);
      Color bkColor = new Color(Display.getDefault(), rgb);
      titleComposite.setBackground(bkColor);// 设置标题背景色
      mainComposite = new Composite(shell,SWT.INHERIT_DEFAULT);
      mainComposite.setBounds(1, 29, WIDTH-2, HEIGHT - 30);
      chromium = new Chromium(mainComposite, SWT.NONE,"about:blank",new ChromeSettings().right_button(false, true));
      chromium.setBounds(0, 0, WIDTH, HEIGHT - 29);
      ybaLl = new Label(titleComposite, SWT.PUSH);
      ybaLl.setFont(new Font(display,"微软雅黑",10,SWT.NORMAL));//设置文字的字体字号
      ybaLl.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
      final Image closeImg = new Image(display, path() + "image/close.png");
      final Image close_hoverImg = new Image(display, path() +"image/close_hover.png");
      final Image close_clickImg = new Image(display, path() +"image/close_click.png");
//      final Image titleImg = new Image(display, path() +"image/title.jpg");
      titleLl = new Label(titleComposite,SWT.PUSH);
      final Image titleLogo = new Image(display, path()+"image/titleLogo.png");
      titleLl.setImage(titleLogo);
      titleLl.setBounds(6,6,15,15);
      closeLl = new Label(titleComposite, SWT.PUSH);
      closeLl.setImage(closeImg);
      closeLl.setBounds(WIDTH-27, 3, 23, 23);
      closeLl.setToolTipText("关闭");
      closeLl.addMouseTrackListener(new MouseTrackListener() {
        
        @Override
        public void mouseHover(MouseEvent arg0) {
          // TODO Auto-generated method stub
          closeLl.setImage(close_hoverImg);
        }
        
        @Override
        public void mouseExit(MouseEvent arg0) {
          // TODO Auto-generated method stub
          closeLl.setImage(closeImg);
        }
        
        @Override
        public void mouseEnter(MouseEvent arg0) {
        }
      });
      
      Display.getCurrent().addFilter(SWT.KeyDown, new Listener() {
        public void handleEvent(Event event) {
            if (event.keyCode == SWT.F4 && (event.stateMask & SWT.ALT) != 0) {
                if (Display.getCurrent().getActiveShell() == shell) {
                    shell.close();
                }
            }
        }
    });
      shell.addListener(SWT.Close, new Listener() {
        
        @Override
        public void handleEvent(Event e) {
          e.doit = false; 
          shell.setVisible(false); 
        }
    });
      
    
      closeLl.addMouseListener(new MouseAdapter() {// 关闭按钮鼠标点击事件监听
        public void mouseUp(MouseEvent e) {
          LoginUI.isOpenDialog="";
          CloseDialog();
        }
        public void mouseDown(MouseEvent e){
          closeLl.setImage(close_clickImg);
        }
      });
      
      move();
    }
    
    
    
    protected void showPage() {  
        if(title.equals("文件夹弹窗")){
          chromium.setUrl("http://127.0.0.1:" + ConfigUtil.getRestServerPort() + "/web/html/client-list.html?dsType=0");
          ybaLl.setText("易备安文件夹同步");
          ybaLl.setBounds(27, 4, 200, 29);
        }else if(title.equals("保险箱弹窗")){
          chromium.setUrl("http://127.0.0.1:" + ConfigUtil.getRestServerPort() + "/web/html/client-safeBox.html");  
          ybaLl.setText("易备安数据保险箱");
          ybaLl.setBounds(27, 4, 120, 29);
        }else if(title.equals("文件弹窗")){
          chromium.setUrl("http://127.0.0.1:" + ConfigUtil.getRestServerPort() + url);
          ybaLl.setText("易备安文件夹同步");
          ybaLl.setBounds(27, 4, 400, 29);
        }
        
    }  
    
    public void CloseDialog(){
      shell.setVisible(false);
    }
    
    
    public void ShowDialog(){
      LoginUI.isOpenDialog="记录锁屏前是否Dialog";
      shell.setVisible(true);
    }
    
    
    /**
     * 窗体拖动
     */
    public void move() {
      Listener listener = new Listener() {
        int startX, startY;

        public void handleEvent(Event e) {
          if (e.type == SWT.MouseDown && e.button == 1) {
            startX = e.x;
            startY = e.y;
          }
          if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
            Point p = shell.toDisplay(e.x, e.y);
            p.x -= startX;
            p.y -= startY;
            shell.setLocation(p);
          }
        }
      };
      titleComposite.addListener(SWT.MouseDown, listener);
      titleComposite.addListener(SWT.MouseMove, listener);
      mainComposite.addListener(SWT.MouseDown, listener);
      mainComposite.addListener(SWT.MouseMove, listener);
      ybaLl.addListener(SWT.MouseDown, listener);
      ybaLl.addListener(SWT.MouseMove, listener);
    }
    
    
//    public void titleListener(){
//      chromium.addTitleChangeListener(new TitleChangeListener() {//监听标题是否发生改变,title是发生跳转后的标题
//        
//        public void TitleChanged(String titleUrl, Chromium c)
//        {
//          String url = titleUrl.split("\\|")[1];
//          LoginUI.isOpenDialogUrl = url;
//        }
//    });
//    }
}  